package com.kb.design.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.util.StringUtil;
import com.kb.design.constant.Constants;
import com.kb.design.dto.Children;
import com.kb.design.entity.DesignDept;
import com.kb.design.entity.DesignPost;
import com.kb.design.entity.DesignUserPost;
import com.kb.design.mapper.postgreSQL.DesignDeptMapper;
import com.kb.design.mapper.postgreSQL.DesignPostMapper;
import com.kb.design.mapper.postgreSQL.DesignUserPostMapper;
import com.kb.design.service.DeptService;
import com.kb.design.service.PostService;
import com.kb.design.util.StringUtils;
import com.kb.design.vo.DesignDeptVO;
import com.kb.design.vo.DesignPostVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Auther:chenzx
 * @Date:2024-04-01
 * @Description:
 */
@Service
@DS("slave-1")
public class PostServiceImpl extends ServiceImpl<DesignPostMapper, DesignPost> implements PostService {


    @Resource
    private DesignPostMapper designPostMapper;
    @Resource
    private DesignUserPostMapper designUserPostMapper;

    @Override
    public List<DesignPost> selectDeptList(DesignPost post) {
        LambdaQueryWrapper<DesignPost> queryWrapper = Wrappers.lambdaQuery();
        if (Objects.nonNull(post.getStatus()) && post.getStatus() !=""){
            queryWrapper.eq(DesignPost::getStatus,post.getStatus());
        }
        if (StringUtil.isNotEmpty(post.getPostName())){
            queryWrapper.like(DesignPost::getPostName,post.getPostName());
        }
        queryWrapper.eq(Objects.nonNull(post.getPostId()),DesignPost::getPostId,post.getPostId());
        return designPostMapper.selectList(queryWrapper);
    }

    @Override
    public DesignPost getPostById(Long id) {
        return designPostMapper.selectById( id);
    }

    @Override
    public DesignPost getPostByUserCode(String id) {
        return  designPostMapper.getPostByUserCode(id);
    }

    @Override
    public void insertPost(DesignPost post) {
        designPostMapper.insert(post);
    }

    @Override
    public long selectPostCount(Long id) {

        return 0;
    }

    @Override
    public Boolean queryPostUser(String id) {
        //根据用户code查询岗位
        List<DesignPost> designPosts = designPostMapper.queryPostUser(id);
        if (CollectionUtil.isNotEmpty(designPosts)){
            for (DesignPost designPost : designPosts) {
                if (designPost.getPostId() == 2){
                    return true;
                }
            }
        }
        return false;
    }

    @Override
    public long deletePostById(Long id) {
        LambdaQueryWrapper<DesignPost> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(DesignPost::getPostId, id);
        DesignPost post = designPostMapper.selectOne(wrapper);
        if (designPostMapper.countUserPostById(id) > 0)
        {
            throw new RuntimeException(String.format("%1$s已分配,不能删除", post.getPostName()));
        }
        return designPostMapper.delete(Wrappers.lambdaQuery(DesignPost.class)
                .eq(DesignPost::getPostId, id));
    }

    @Override
    public boolean checkPostNameUnique(DesignPost post) {
        if (StringUtils.isNotEmpty(post.getPostName())) {
            DesignPost info = designPostMapper.selectOne(Wrappers.lambdaQuery(DesignPost.class)
                    .eq(DesignPost::getPostName, post.getPostName()));
            if (Objects.nonNull(info)) {
                return Constants.NOT_UNIQUE;
            }
        }
        return Constants.UNIQUE;
    }

    @Override
    public int updatePost(DesignPost post) {
        return designPostMapper.update(post, Wrappers.lambdaUpdate(DesignPost.class)
                .eq(DesignPost::getPostId, post.getPostId()));
    }

    @Override
    public Children queryPostUser() {
        Children children = new Children();
        // 查询所有岗位数据 status = Constants.DEL_FLAG_NO 的数据
        List<DesignPost> designPosts = designPostMapper.selectList(Wrappers.lambdaQuery(DesignPost.class)
                .eq(DesignPost::getStatus, Constants.DEL_FLAG_NO));
        // 查询所有人员
        List<DesignUserPost> designUserPosts = designUserPostMapper.queryAllUserPost();
        children.setLabel("岗位");
        List<Children> childrenList = designPosts.stream()
                .map(designPost -> {
                    Children childrenPost = new Children();
                    childrenPost.setLabel(designPost.getPostName());
                    childrenPost.setValue(designPost.getPostId().toString());
                    List<Children> childrenUserList = designUserPosts.stream()
                            .filter(designUserPost -> Objects.nonNull(designUserPost.getPostId())
                                    && designUserPost.getPostId().equals(designPost.getPostId()))
                            .map(designUserPost -> {
                                Children childrenUser = new Children();
                                childrenUser.setLabel(designUserPost.getName());
                                childrenUser.setValue(designUserPost.getCode());
                                return childrenUser;
                            })
                            .collect(Collectors.toList());
                    childrenPost.setChildren(childrenUserList);
                    return childrenPost;
                })
                .collect(Collectors.toList());
        children.setChildren(childrenList);
        return children;
    }
}
